#cmake file for examples
#author:guyadong
#created:2020/04/02
# 测试程序

#########################
# 设置项目属性
function (define_redis_example_target target)
  if(NOT TARGET ${target})
    return ()
  endif()
  set_target_properties (${target}  PROPERTIES 
    FOLDER examples
    DEBUG_POSTFIX _d
    )
  # 因为大量使用模板,导致代码增大,MSVC下要用/bigobj选项,否则会报错:error C1128
  # GNU compiler 要用 -Wa,-mbig-obj 选项
  target_compile_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
      $<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>)
  target_link_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/ignore:4099> )
  set(_link_libraries cpp_redis::cpp_redis ${SMQ_LIBNAME})
  target_link_libraries(${target} ${_link_libraries})
  unset(_link_libraries)
endfunction()

# 设置项目属性
function (define_mqtt_example_target target)
  if(NOT TARGET ${target})
    return ()
  endif()
  set_target_properties (${target}  PROPERTIES 
    FOLDER examples
    DEBUG_POSTFIX _d
    )
  # 因为大量使用模板,导致代码增大,MSVC下要用/bigobj选项,否则会报错:error C1128
  # GNU compiler 要用 -Wa,-mbig-obj 选项
  target_compile_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
      $<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>)
  target_link_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/ignore:4099> )
  set(_link_libraries PahoMqttCpp::paho-mqttpp3-static ${SMQ_LIBNAME})
  target_link_libraries(${target} ${_link_libraries})
  unset(_link_libraries)
endfunction()

find_package(cpp_redis MODULE QUIET)

if( TARGET cpp_redis::cpp_redis )

  add_executable(test_redis  test_redis.cpp)
  add_executable(cppredis_sub  cppredis_sub.cpp)
  add_executable(redis_subscriber  redis_subscriber.cpp)
  add_executable(redis_client  redis_client.cpp)
  add_executable(redis_publish  redis_publish.cpp)

  define_redis_example_target(test_redis)
  define_redis_example_target(cppredis_sub)
  define_redis_example_target(redis_subscriber)
  define_redis_example_target(redis_client)
  define_redis_example_target(redis_publish)
endif()

find_package(PahoMqttCpp QUIET)

if(TARGET PahoMqttCpp::paho-mqttpp3-static)
  message(STATUS "target PahoMqttCpp::paho-mqttpp3-static FOUND")
else()
  message(STATUS "target PahoMqttCpp::paho-mqttpp3-static NOT FOUND") 
endif()


if( TARGET PahoMqttCpp::paho-mqttpp3-static)

  add_executable(mqtt_subscriber  mqtt_subscriber.cpp)
  add_executable(mqtt_client  mqtt_client.cpp)
  add_executable(mqtt_publish  mqtt_publish.cpp)

  define_mqtt_example_target(mqtt_subscriber)
  define_mqtt_example_target(mqtt_client)
  define_mqtt_example_target(mqtt_publish)
endif()
#####################

